Package aspect.editor

Source Code of aspect.editor.EditorControl

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aspect.editor;

import static aspect.core.AspectLauncher.*;
import static aspect.core.AspectRenderer.*;
import aspect.editor.LevelEditor.Tool;
import aspect.entity.Entity;
import aspect.entity.Player;
import aspect.entity.behavior.Behavior;
import aspect.event.KeyEvent;
import aspect.event.MouseEvent;
import aspect.render.Material;
import aspect.render.shader.Shader;
import aspect.render.shader.Shader;
import aspect.render.shader.ShaderProgram;
import static aspect.resources.Resources.*;
import aspect.util.Angles;
import aspect.util.Color;
import aspect.util.Vector3;
import aspect.world.ListWorld;
import aspect.world.World;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelEvent;
import java.io.File;
import java.nio.FloatBuffer;
import javax.swing.JList;
import org.lwjgl.BufferUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

/**
*
* @author vincent
*/
public class EditorControl extends Behavior {

    public boolean update;
    private Entity cube;

    private View2D xy;
    private View2D zy;
    private View2D xz;
    private View3D xyz;

    private JList items;

    private LevelEditor editor;

    public final CubeBuilder builder = new CubeBuilder();
    private ListWorld world;
    private Material m;

    private ShaderProgram shader;

    public EditorControl(LevelEditor editor) {
        this.editor = editor;
        this.xy = editor.getViewXY();
        this.zy = editor.getViewYZ();
        this.xz = editor.getViewXZ();
        this.xyz = editor.getViewXYZ();
        this.items = editor.getEntityList();

        xy.control = this;
        zy.control = this;
        xz.control = this;
    }

    @Override
    public void onAdd() {
        setCulling(true);
        world = new ListWorld();
        System.out.println("OnAdd");
        m = new Material(loadTexture(new File("textures/tex.jpg")));
        m.setTextureFilter(Material.Filter.MIPMAP, Material.Filter.LINEAR);

        shader = ShaderProgram.COLOR;

        cube = new EditorBlock(shader, m, new Vector3(-5, -4, -10), new Vector3(5, -2, -5));
        world.add(cube);
        cube.name = "Cube 1";

        updateList();
        addMouseListener(this);
        addKeyListener(this);
       
        MaterialSelector selector = editor.getMaterialSelector();

    }

    @Override
    public void update() {

        if (Mouse.isButtonDown(1)) {
            if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
                moveCameraUp(Mouse.getDY() / 20.0f);
                strafeCameraLeft(Mouse.getDX() / 20.0f, false);
            }
            rotateCameraLeft(Mouse.getDX() / 2);
            rotateCameraUp(Mouse.getDY() / 2);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
            moveCameraForward(0.1f, false);
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
            moveCameraBackward(0.1f, false);
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
            strafeCameraLeft(0.1f, true);
        }

        if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
            strafeCameraRight(0.1f, true);
        }
    }

    @Override
    public void mouseEvent(MouseEvent evt) {
        if (evt.wheel != 0) {
            moveCameraForward(evt.wheel / 120f, false);
        }

        if (evt.button == 0 && evt.type) {
            EditorBlock.selectionMode = true;
            world.draw();
            FloatBuffer buff = BufferUtils.createFloatBuffer(3);
            GL11.glReadPixels(evt.x, evt.y, 1, 1, GL11.GL_RGB, GL11.GL_FLOAT, buff);
            Color c = new Color(buff.get(), buff.get(), buff.get());
            EditorBlock block = EditorBlock.select(c);
            if (block != null) {
                items.setSelectedValue((block), false);
            } else {
                items.setSelectedIndices(new int[0]);
            }

        }
    }

    @Override
    public void keyEvent(KeyEvent evt) {
        if (evt.key == Keyboard.KEY_RETURN && evt.type && !builder.min.equals(builder.max)) {
            Entity ent = new EditorBlock(shader, m, builder.min, builder.max);
            world.add(ent);
            ent.name = "Cube " + world.size();

            updateList();
            builder.max = Vector3.zero();
            builder.min = Vector3.zero();
           
            items.setSelectedValue(ent, false);
        } else if (evt.key == Keyboard.KEY_DELETE && evt.type) {
            Entity value = (Entity) items.getSelectedValue();
            if (value != null) {
                world.remove(value);
                updateList();
            }
        }
    }

    private void updateList() {
        Entity[] ents = world.toArray();
        Object[] objs = new Object[ents.length];
        System.arraycopy(ents, 0, objs, 0, ents.length);
        items.setListData(objs);
    }

    public void setup2D() {
        xy.setupDraw();
        zy.setupDraw();
        xz.setupDraw();
    }

    public void draw2D(Drawable2D block) {
        xy.drawBlock(block);
        zy.drawBlock(block);
        xz.drawBlock(block);
    }

    public void paint2D() {
        xy.repaint();
        zy.repaint();
        xz.repaint();
    }

    @Override
    public void render() {
        EditorBlock.selectionMode = false;
        setup2D();
        for (Entity ent : world) {
            if (ent instanceof EditorBlock) {
                draw2D((EditorBlock) ent);
            }
        }
        draw2D(builder);
        paint2D();
        world.draw();
        builder.draw();
    }

}
TOP

Related Classes of aspect.editor.EditorControl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.